import { getHomeGoodsGuessLikeAPI } from '@/services/home'
import type { PageParams } from '@/types/global'
import type { GuessItem } from '@/types/home'
import { onMounted, ref } from 'vue'

const useXtxGuessHooks = () => {
  // 分页参数
  const pageParams: Required<PageParams> = {
    page: 1,
    pageSize: 10,
  }

  // 猜你喜欢的列表
  const guessList = ref<GuessItem[]>([])

  // 已结束标记
  const finish = ref(false)

  // 获取猜你喜欢数据
  const getHomeGoodsGuessLikeData = async () => {
    // 退出分页判断
    if (finish.value) {
      return uni.showToast({ icon: 'none', title: '没有更多数据~' })
    }
    const res = await getHomeGoodsGuessLikeAPI(pageParams)
    // 数组追加
    guessList.value.push(...res.result.items)
    // 分页条件
    if (pageParams.page < res.result.pages) {
      // 页码累加
      pageParams.page++
    } else {
      finish.value = true
    }
  }

  // 重置数据
  const resetData = () => {
    pageParams.page = 1
    guessList.value = []
    finish.value = false
  }

  // 组件挂载获取服务端数据
  onMounted(() => {
    getHomeGoodsGuessLikeData()
  })

  return {
    finish,
    guessList,
    resetData,
    getHomeGoodsGuessLikeData,
  }
}

export default useXtxGuessHooks
